--
-- Author: yu.yu
-- Date: 2018-11-10
--
-- BossTeamRankLegionRender
--
local Util = require("app.utils.Util")
local AvatarFactotry = require("app.public.avatar.AvatarFactory")
local HeadRender = require("app.widget.renders.HeadRender")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.BossTeamLegionRankUI))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui.Panel:getContentSize())

    self:refresh()
end

function ClassRef:refresh()
    if not self._data then return end

    local members = self._data
    local panelTeam = self._ui.Panel

    for i = 1, 10 do
        local player = panelTeam:getChildByName("player"..i)
        if members[i] then
            local baseInfo =Util.createUserBaseInfoStruct( members[i].userBaseInfo)
            player:findChild("Panel/Panel_withData"):setVisible(true)

            local h = HeadRender.create(player:findChild("Panel/Panel_withData/head"), 3)
            h:setData( cloneWithoutMeta(baseInfo) )
            h:onClick(function(sender)
                if baseInfo.uid ~= UD.player.uid then --点击别人的头像
                    local size = sender:getContentSize()
				    local pos = sender:convertToWorldSpace(cc.p(size.width, size.height/2))
				    UIHandler:showHeadMenu({online=true, attr=baseInfo, pos=pos})
                end
            end)

            player:findChild("Panel/Panel_withData/name"):setString(L(baseInfo.name))
            if not members[i].isLeader then
                player:findChild("Panel/Panel_withData/name"):setTextColor(cc.c3b(108,158,239))
            else
                player:findChild("Panel/Panel_withData/name"):setTextColor(cc.c3b(255,255,255))
            end
            player:findChild("Panel/Panel_withData/lv_num"):setString(baseInfo.combatPower)
            --设置战力图片和文字的位置
            local lvPosX = player:findChild("Panel/Panel_withData/lv_num"):getPositionX()
            local lvWidth = player:findChild("Panel/Panel_withData/lv_num"):getContentSize().width
            player:findChild("Panel/Panel_withData/combat_icon"):setPositionX( math.floor(lvPosX - lvWidth/2))

            player:findChild("Panel/Panel_withData/icon_leader"):setVisible(members[i].isLeader)
            player:findChild("Panel/Panel_withData/Image_Forbidden"):setVisible(false)                    
            player:findChild("Panel/Panel_withData/partner"):setVisible(false)

        else
            player:findChild("Panel/Panel_withData"):setVisible(false)
        end

    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
